﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using Digitalmes.Model.ProductManagement;
using Digitalmes.Model;

namespace Digitalmes.ViewModel.ProductManagement.ProductVMs
{
    public partial class ProductListVM : BasePagedListVM<Product_View, ProductSearcher>
    {
        
        protected override IEnumerable<IGridColumn<Product_View>> InitGridHeader()
        {
            return new List<GridColumn<Product_View>>{
                
                this.MakeGridHeader(x => x.Product_Name).SetTitle(@Localizer["Page.产品名称"].Value),
                this.MakeGridHeader(x => x.Product_Code).SetTitle(@Localizer["Page.产品编码"].Value),
                this.MakeGridHeader(x => x.Product_ErpCode).SetTitle(@Localizer["Page.Erp编号"].Value),
                this.MakeGridHeader(x => x.Product_Material).SetTitle(@Localizer["Page.物料"].Value),
                this.MakeGridHeader(x => x.Product_Active).SetTitle(@Localizer["Page.激活"].Value),
                this.MakeGridHeader(x => x.Product_CreateTime).SetTitle(@Localizer["_Admin.CreateTime"].Value),
                this.MakeGridHeader(x => x.Product_UpdateTime).SetTitle(@Localizer["_Admin.UpdateTime"].Value),
                this.MakeGridHeader(x => x.Product_CreateBy).SetTitle(@Localizer["_Admin.CreateBy"].Value),
                this.MakeGridHeader(x => x.Product_UpdateBy).SetTitle(@Localizer["_Admin.UpdateBy"].Value),

                this.MakeGridHeaderAction(width: 200)
            };
        }

        
        public override IOrderedQueryable<Product_View> GetSearchQuery()
        {
            var query = DC.Set<Product>()
                
                .CheckContain(Searcher.Name, x=>x.Name)
                .CheckContain(Searcher.Code, x=>x.Code)
                .CheckContain(Searcher.ErpCode, x=>x.ErpCode)
                .CheckEqual(Searcher.MaterialId, x=>x.MaterialId)
                .CheckEqual(Searcher.Active, x=>x.Active)
                .CheckBetween(Searcher.CreateTime?.GetStartTime(), Searcher.CreateTime?.GetEndTime(), x => x.CreateTime, includeMax: false)
                .CheckBetween(Searcher.UpdateTime?.GetStartTime(), Searcher.UpdateTime?.GetEndTime(), x => x.UpdateTime, includeMax: false)
                .CheckContain(Searcher.CreateBy, x=>x.CreateBy)
                .CheckContain(Searcher.UpdateBy, x=>x.UpdateBy)
                .Select(x => new Product_View
                {
				    ID = x.ID,
                    
                    Product_Name = x.Name,
                    Product_Code = x.Code,
                    Product_ErpCode = x.Material.ErpCode,
                    Product_Material = x.Material.Name,
                    Product_Active = x.Active,
                    Product_CreateTime = x.CreateTime,
                    Product_UpdateTime = x.UpdateTime,
                    Product_CreateBy = x.CreateBy,
                    Product_UpdateBy = x.UpdateBy,
                })
                .OrderBy(x => x.ID);
            return query;
        }

        
    }
    public class Product_View: Product
    {
        
        public string Product_Name { get; set; }
        public string Product_Code { get; set; }
        public string Product_ErpCode { get; set; }
        public string Product_Material { get; set; }
        public bool? Product_Active { get; set; }
        public DateTime? Product_CreateTime { get; set; }
        public DateTime? Product_UpdateTime { get; set; }
        public string Product_CreateBy { get; set; }
        public string Product_UpdateBy { get; set; }

    }

}